package com.starblink.search.category.ui.main.tab

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.fragment.BaseTVMLazyLoadingFragment
import com.starblink.android.basic.ext.autoLoadAndRefresh
import com.starblink.android.basic.ext.bindProductFeeds
import com.starblink.android.basic.ext.dp2px
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.basic.ext.screenHeight
import com.starblink.basic.route.RoutePage
import com.starblink.library.widget.loading.GoodsCardLoadingCell
import com.starblink.library.widget.product.ProductCardScene
import com.starblink.library.widget.product.SimpleProductCardCell
import com.starblink.search.category.ui.main.cell.ThreeLevelSpanCell
import com.starblink.search.databinding.CellCategoryThreeSpanBinding
import com.starblink.search.databinding.FgmCategoryMainGoodsBinding
import com.starblink.search.result.ui.cell.GoodsEmptyCell

/**
 * 类目tab 页
 */
internal class CategoryGoodsTabFragment :
    BaseTVMLazyLoadingFragment<FgmCategoryMainGoodsBinding, CategoryTabVM>() {

    private val adapter by lazyOnNone {
        MutableAdapter(viewModel.data).apply {
            addVhDelegate {
                SimpleProductCardCell(
                    viewBinding.layout.refresher,
                    viewBinding.layout.recycler,
                    it,
                    ProductCardScene.CATEGORY_MAIN
                )
            }
            addVhDelegate {
                ThreeLevelSpanCell(
                    viewModel.categoryId ?: "",
                    viewModel.categoryName ?: "",
                    CellCategoryThreeSpanBinding.inflate(layoutInflater, it, false)
                )
            }
            addVhDelegate {
                GoodsCardLoadingCell(it)
            }
            addVhDelegate {
                GoodsEmptyCell(it)
            }
        }
    }

    private var scrolledY = 0
    private var index = 0

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FgmCategoryMainGoodsBinding = FgmCategoryMainGoodsBinding.inflate(layoutInflater)

    override fun initData() {
        viewModel.categoryId = arguments?.getString(RoutePage.ARG.ID)
        viewModel.categoryName = arguments?.getString(RoutePage.ARG.TITLE)
        index = arguments?.getInt(RoutePage.Product.INDEX, 0)!!
    }

    override fun initView(view: View) {
        showStateSuccess()

        if (index == 0) {
            onLazyInitData()
        }
        viewBinding.layout.refresher.autoLoadAndRefresh(refresh = {
            viewModel.pageNo = 1
            onLazyInitData()
        }) {
            viewModel.pageNo++
            viewModel.queryGoods()
        }
        viewBinding.layout.refresher.setEnableRefresh(false)
        viewBinding.layout.refresher.setEnableLoadMore(false)
        viewBinding.layout.recycler.bindProductFeeds(adapter)
//        SimilarHeaderCollapseDecorator.attach(viewBinding.recycler, mainPrdBinding.root)
        viewBinding.layout.recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                scrolledY += dy
                viewBinding.btnTop.visibleOrGone(scrolledY > screenHeight.shr(1))
            }
        })
        viewBinding.btnTop.setOnClickListener {
            viewBinding.layout.recycler.smoothScrollBy(0, -scrolledY)
        }

//        showOrHideNavBarAnim("${this.javaClass.name}$index", viewBinding.layout.recycler)
        viewBinding.layout.refresher.bindHomeBar()
    }

    override fun initLoad() {

    }

    override fun onLazyInitData() {
        viewModel.queryCategories()
        viewModel.queryGoods()
    }

    override fun startObserve() {
        viewModel.dataLD.observe(this) {
            viewBinding.layout.refresher.setEnableRefresh(true)
            viewBinding.layout.refresher.setEnableLoadMore(true)
            viewBinding.layout.refresher.finishRefresh()
            if (it.isNullOrEmpty()) {
                viewBinding.layout.refresher.finishLoadMoreWithNoMoreData()
            } else {
                viewBinding.layout.refresher.finishLoadMore()
                val idxTag = adapter.itemCount
                viewModel.data.addAll(it)
                if (viewModel.pageNo == 1) {
                    adapter.notifyItemRangeChanged(idxTag, it.size)
                } else {
                    adapter.notifyItemRangeInserted(idxTag, it.size)
                }
            }
        }
        viewModel.categoryLD.observe(this) {
            adapter.notifyItemChanged(0)
        }
    }


    companion object {

        fun newInstance(categoryId: String?, categoryName: String?, index: Int = 0) =
            CategoryGoodsTabFragment().apply {
                arguments = bundleOf(
                    RoutePage.Product.INDEX to index,
                    RoutePage.ARG.ID to categoryId,
                    RoutePage.ARG.TITLE to categoryName,
                )
            }

    }

}